<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.File"%>
<%@ page import="java.io.BufferedReader"%>
<%@ page import="org.apache.commons.lang.StringEscapeUtils"%>
<%@ page
	import="org.apache.commons.collections.buffer.CircularFifoBuffer"%>
<%@ page import="java.io.FileReader"%>
<%@ page import="de.home.util.Configuration"%>
<!DOCTYPE>
<html>
<head>
<title>Home Controller</title>
<link rel="stylesheet" type="text/css" href="css/generalStyle.css">
<link rel="stylesheet" type="text/css" href="css/styleLog.css">
<script src="js/jquery-2.1.1.js"></script>
<script src="js/generalScript.js" type="text/javascript"></script>
<script src="js/scriptLog.js" type="text/javascript"></script>
</head>
<body>
	<div id="title">
		<div>
			<a id="top" href="index.jsp"></a>
			<a id="normal"></a>
			<h1>Log-Ausgaben</h1>
		</div>
	</div>
	<div id="errorBox"></div>
	<div id="infoBox"></div>
	<div id="warningBox"></div>
	<%
	   int n = Configuration.getInstance().getInt("logNLines", 20);
	   if ( request.getParameter("nLines") != null ) {
	      try {
	         n = Integer.parseInt(request.getParameter("nLines"));
	      } catch (NumberFormatException e) {
	         n = Configuration.getInstance().getInt("logNLines", 20);
	      }
	   }
	%>

	<header>
		<form action="log.jsp" method="post">
			<input type="text" name="nLines" placeholder="N log Zeilen"
				class="button" id="showNLogsInput" value="<%=n%>" /> <input
				type="submit" value="Anzeigen" class="button" id="showLogsButton" />
		</form>
	</header>
	<%
	   File logFile = new File(System.getProperty("user.home") + "/homeController.log");
	   BufferedReader bufferedReader = new BufferedReader(new FileReader(logFile));
	   String line = bufferedReader.readLine();
	   CircularFifoBuffer fifoBuffer = new CircularFifoBuffer(n);
	   while (line != null) {
	      fifoBuffer.add(line);
	      line = bufferedReader.readLine();
	   }
	   bufferedReader.close();

	   for (Object oneLine : fifoBuffer) {
	      String logEntry = (String) oneLine;
	      logEntry = StringEscapeUtils.escapeHtml(logEntry);
	      logEntry = logEntry.replace(" ", "&nbsp;");

	      if ( logEntry.contains("DEBUG") ) {
	         logEntry = logEntry.replaceFirst("DEBUG", "<p>DEBUG</p>");
	         out.print("<label class=\"logEntry debug\">" + logEntry + "</label>");
	      } else if ( logEntry.contains("INFO") ) {
	         logEntry = logEntry.replaceFirst("INFO", "<p>INFO</p>");
	         out.print("<label class=\"logEntry info\">" + logEntry + "</label>");
	      } else if ( logEntry.contains("WARN") ) {
	         logEntry = logEntry.replaceFirst("WARN", "<p>WARN</p>");
	         out.print("<label class=\"logEntry warn\">" + logEntry + "</label>");
	      } else if ( logEntry.contains("ERROR") ) {
	         logEntry = logEntry.replaceFirst("ERROR", "<p>ERROR</p>");
	         out.print("<label class=\"logEntry error\">" + logEntry + "</label>");
	      } else if ( logEntry.contains("FATAL") ) {
	         logEntry = logEntry.replaceFirst("FATAL", "<p>FATAL</p>");
	         out.print("<label class=\"logEntry fatal\">" + logEntry + "</label>");
	      }

	   }

	   out.flush();
	%>
</body>
</html>